<template>
  <PageWrapper dense contentFullHeight fixedHeight contentClass="flex">
    <BasicTable @register="registerTable" :searchInfo="searchInfo">
      <template #toolbar>
        <a-button type="primary" v-if="aAccessData?.update" @click="handleCreate"
          >新增用户</a-button
        >
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <a-button type="link" v-if="aAccessData?.update" @click="handleEdit(record)" size="small"
            >编辑</a-button
          >
          <a-button type="link" v-if="aAccessData?.edit" @click="fnEditPsd(record)" size="small"
            >改密</a-button
          >
          <a-button
            v-if="record.status === 0"
            type="link"
            danger
            @click="handleDelete(record)"
            size="small"
            >删除</a-button
          >
        </template>
      </template>
    </BasicTable>
    <AccountModal @register="registerModal" @success="handleSuccess" />
  </PageWrapper>
</template>

<script lang="ts" setup>
  import { onBeforeMount, reactive, ref } from 'vue';
  import { BasicTable, useTable } from '@/components/Table';
  import { PageWrapper } from '@/components/Page';
  import { useModal } from '@/components/Modal';
  import { useMessage } from '@/hooks/web/useMessage';
  import { fnApiGetAccessMenu } from '@/utils/auth';
  import { getAccountList, delAccountItem } from '@/api/system/account';
  import { columns, searchFormSchema } from './accounts.data';
  import AccountModal from './AccountModal.vue';

  const { createMessage, createConfirm } = useMessage();
  const [registerModal, { openModal }] = useModal();
  const searchInfo = reactive<Recordable>({});
  const [registerTable, { reload, updateTableDataRecord }] = useTable({
    title: '用户列表',
    api: getAccountList,
    rowKey: 'id',
    columns,
    formConfig: {
      labelWidth: 80,
      schemas: searchFormSchema,
      autoSubmitOnEnter: true,
      showAdvancedButton: false,
      actionColOptions: {
        span: 6,
        style: { marginLeft: '20px' },
      },
    },
    useSearchForm: true,
    showTableSetting: true,
    bordered: true,
    handleSearchInfoFn(info) {
      return info;
    },
    afterFetch: (res) => {
      res.map((item) => {
        item.edit_status = !!aAccessData.value.status;
      });
      return res;
    },
    actionColumn: {
      width: 180,
      title: '操作',
      dataIndex: 'action',
      fixed: undefined,
    },
  });

  const aAccessData: any = ref();

  onBeforeMount(async () => {
    aAccessData.value = await fnApiGetAccessMenu();
  });

  function handleCreate() {
    openModal(true, {
      isUpdate: false,
    });
  }

  function handleEdit(record: Recordable) {
    record.sEditType = '2';
    openModal(true, {
      record,
      isUpdate: true,
    });
  }

  function fnEditPsd(record: Recordable) {
    record.sEditType = '3';
    openModal(true, {
      record,
      isUpdate: true,
    });
  }

  function handleDelete(record: Recordable) {
    createConfirm({
      iconType: 'warning',
      title: '提示',
      content: `确认要删除当前账号吗?`,
      onOk: async () => {
        await delAccountItem({ id: record.id });
        createMessage.success(`删除成功`);
        handleSuccess({ isUpdate: false, values: {} });
        await reload();
      },
    });
  }

  function handleSuccess({ isUpdate, values }) {
    if (isUpdate) {
      // 演示不刷新表格直接更新内部数据。
      // 注意：updateTableDataRecord要求表格的rowKey属性为string并且存在于每一行的record的keys中
      const result = updateTableDataRecord(values.id, values);
      console.log(result);
    } else {
      reload();
    }
  }
</script>
